package com.zzgele.qmzb.api.core;

import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Condition;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 基于通用MyBatis Mapper插件的Service接口的实现
 */
@SuppressWarnings("unchecked")
public abstract class AbstractService<T> implements Service<T> {
   // protected final Logger log = LoggerFactory.getLogger(this.modelClass);

    @Autowired
    protected Mapper<T> mapper;

    private Class<T> modelClass;    // 当前泛型真实类型的Class

    public AbstractService() {
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        modelClass = (Class<T>) pt.getActualTypeArguments()[0];
    }


    public int save(T model) {
        return mapper.insertSelective(model);
    }

    public int save(List<T> models) {
        return mapper.insertList(models);
    }

    public int deleteById(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int deleteByIds(String ids) { return mapper.deleteByIds(ids); }

    public int update(T model) {
        return mapper.updateByPrimaryKeySelective(model);
    }

    public T findById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public T findBy(String fieldName, Object value) throws TooManyResultsException {
        try {
            T model = modelClass.newInstance();
            Field field = modelClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(model, value);
            return mapper.selectOne(model);
        } catch (ReflectiveOperationException e) {
            throw new ServiceException(e.getMessage(), e);
        }
    }

    public List<T> findByIds(String ids) {
        return mapper.selectByIds(ids);
    }

    public List<T> findByCondition(Condition condition) {
        return mapper.selectByCondition(condition);
    }

    public List<T> findAll() {
        return mapper.selectAll();
    }

    public int deleteByCondition(Condition condition) { return mapper.deleteByCondition(condition); }
    public int selectCount(T model){ return mapper.selectCount(model); };
    public int selectCountByCondition(Condition condition) { return mapper.selectCountByCondition(condition); };

    public int deleteById(Long id) { return mapper.deleteByPrimaryKey(id); }
    public T findById(Long id) { return mapper.selectByPrimaryKey(id); }

    //获取列表 add by martin@20170711
    public List<HashMap> getList(Map<String, Object> map) {
        return mapper.getList(map);
    }

    //更新某字段 add by martin@20170708
    public int setInc(Map<String, Object> map) { return mapper.setInc(map);}
    public int setDec(Map<String, Object> map) { return mapper.setDec(map);}

    //获取首页-关注列表 add by lcb@20170908
    public List<HashMap> getIndexFollowList(Map<String, Object> map) {
        return mapper.getIndexFollowList(map);
    }

    //榜单-粉丝榜 add by lcb@20170908
    public List<HashMap> getFansRankList() {
        return mapper.getFansRankList();
    }

    //榜单-土豪榜 add by lcb@20170908
    public List<HashMap> getLocalTyrantRankList(){
        return mapper.getLocalTyrantRankList();
    }

    //更新某字段 add by lcb@20170909
    public int setSpe(Map<String, Object> map) { return mapper.setSpe(map);}
    public int setTtd(Map<String, Object> map) { return mapper.setTtd(map);}
    public int setTic(Map<String, Object> map) { return mapper.setTic(map);}
    public int UpdatePrivate(Map<String, Object> map) { return mapper.UpdatePrivate(map);}
    public int UpdateBroadcast(Map<String, Object> map) { return mapper.UpdateBroadcast(map);}
    //充值记录 add by lcb@20170911
    public List<HashMap> getRechargeLogList(Map<String, Object> map){
        return mapper.getRechargeLogList(map);
    }

    //直播间-粉丝贡献榜
    public List<HashMap> getContributionRankList(Map<String, Object> map){
        return mapper.getContributionRankList(map);
    }
    //本月收入民票
    public String getMonthTotalTicket(Map<String, Object> map){
        return mapper.getMonthTotalTicket(map);
    }
    //获取列表 add by jack
    public List<HashMap> getForFamilyList(Map<String, Object> map){
        return mapper.getForFamilyList(map);
    }
    public     int getListSize(Map<String, Object> map){
        return mapper.getListSize(map);

    }


}
